//
//  MStatusViewModel.swift
//  iOSweibo
//
//  Created by lzm on 16/5/17.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit

class MStatusViewModel: NSObject {
    static let sharedInstance : MStatusViewModel = MStatusViewModel()
    //私有构造函数
    override init() {
        super.init()
        
    }
    
    
    ///加载微博数据
    func loadStatus(loadStatusCallBack : (statuses:[MStatus]? , error:NSError?) -> ())
    {
        //获取微博信息
        guard let access_token = MUserAccountViewModel.sharedInstance.userAccount?.access_token else
        {
            MPrint(items: "加载不到access_token数据")
            return
        }
            let url = "2/statuses/friends_timeline.json"
            let parameters = ["access_token" : access_token]
            
            MNetworkTool.sharedInstance.request(RequestMethod.GET, URLString: url, parameters: parameters, success: { (_, responseObject) -> Void in
                MPrint(items: "token:\(access_token)")
                //获得微博的数据
                if let resutle = responseObject as? [String : AnyObject] {
                    //拿到微博数据里面的statuses
                    if let statuses = resutle["statuses"] as? [[String : AnyObject]]{
                        
                        //定义空数组
                        var statuArray = [MStatus]()
                        
                        for dic in statuses
                        {
                            let statusModel = MStatus(dict: dic)
                            statuArray.append(statusModel)
                        }
//                        MPrint(items: "获取模型:\(statuArray)")
                        loadStatusCallBack(statuses: statuArray, error: nil)
                    }
                }
                }, failure: { (_, error) -> Void in
//                    MPrint(items: "获取模型失败:\(error)")
                    loadStatusCallBack(statuses: nil, error: error)
            })
    }
}

